<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">自定义内容列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">自定义内容添加</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>奖品类型</th>
                                <th>奖品名称</th>
                                <th>默认库存</th>
                                <th>当前库存</th>
                                <th>默认用户人数</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {volist name="returnData"  id="list"}
                            <tr>
                                <td>{$list.key}</td>
                                <td>{$list.type}</td>
                                <td>{$list.gift_name}</td>
                                <td>{$list.default_stock}</td>
                                <td>{$list.prize_stock}</td>
                                <td>{$list.sum}</td>
                                {php}if(!empty($list['sum']))
                                echo "<td>
                                <button class='sign-rule-member btn btn-warning btn-sm btn-margin' >查看成员</button>
                                <button class='sign-rule-member-edit btn btn-warning btn-sm btn-margin'>修改</button>
                                <button class='sign-rule-member-del btn btn-warning btn-sm btn-margin'>删除</button>
                            </td>";
                                else
                                echo "<td>
                                <button class='sign-rule-member-edit btn btn-warning btn-sm btn-margin'>修改</button>
                                <button class='sign-rule-member-del btn btn-warning btn-sm btn-margin'>删除</button>
                            </td>";
                                {/php}
                            </tr>
                            {/volist}
                            </tbody>
                        </table>
                        <input id="redis-key" type="hidden" name="" value="{$redisKey}">
                    </div>

                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal" enctype="" id="form" >
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gift-type">
                                        <span class="c-red">*</span>奖励类型：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="type" id="gift-type">
                                            <option value="">--请选择--</option>
                                            {volist name='giftType' id='gift'}
                                            <option value="{$gift['k']}">{$gift['v']}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="typed-id">
                                        <span class="c-red">*</span>奖品名称：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="type_id" id="typed-id">
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="default_stock">
                                        <span class="c-red">*</span>默认库存：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" name="default_stock" id="default_stock">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="prize_stock">
                                        <span class="c-red">*</span>当前库存：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" name="prize_stock" id="prize_stock">
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label class="col-sm-2 control-label">*默认人员</label>
                                    <div class="col-sm-10">
                                        <textarea name="default_user" id="default_user" cols="30" rows="10"  placeholder="填写用户u_id,并用逗号隔开"></textarea>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-2 col-sm-10">
                                        <input id="" type="hidden" name="redisKey" value="{$redisKey}">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" id="btn-submit">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}

<script>
    var defaultUserObj = $('.sign-rule-member');
    var editDefineContentObj = $('.sign-rule-member-edit');
    var delDefineContentObj = $('.sign-rule-member-del');
    //默认人员列表
    if(defaultUserObj.length){
        defaultUserObj.on('click',function(){
            var arrKey = $(this).parent().siblings('td').eq(0).text();
            var redisKey = $('#redis-key').val();
//            redisKey -= 1;
            layer.open({
                type: 2,
                title: ['默认成员信息','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['700px', '500px'],
                content:"/SignRuleDefaultUserIndex?arrKey="+ arrKey + "&redisKey=" + redisKey,
                offset:'auto',
                scrollbar:false
            });
        });
    }
    //自定义内容修改
    editDefineContentObj.on('click',function(){
        var arrKey = $(this).parent().siblings('td').eq(0).text();
        var defaultStock = $(this).parent().siblings('td').eq(3).text();
        var prizeStock = $(this).parent().siblings('td').eq(4).text();
        var redisKey = $('#redis-key').val();
      // redisKey -= 1;
        layer.open({
            type: 2,
            title: ['修改默认内容','font-size:18px;font-family:宋体;font-weight:700;'],
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['700px', '500px'],
            content:"/SignRuleEditDefineContent?arrKey="+ arrKey + "&redisKey=" + redisKey+"&defaultStock=" + defaultStock+"&prizeStock=" + prizeStock,
            offset:'auto',
            scrollbar:false,
            end: function () {
                location.reload();
            }
        });
    });

    //删除自定义内容
    delDefineContentObj.click(function(){
            var arrKey = $(this).parent().siblings('td').eq(0).text();
            var redisKey = $('#redis-key').val();
            layer.confirm('确定删除吗？',{
                title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
                btn: ['确定','取消'] //按钮
            },function(){
                $.ajax({
                    type:'get',
                    url:"/SignRuleDelDefineContent",
                    async:true,
                    data:{arrKey:arrKey,redisKey:redisKey},
                    dataType:'json',
                    success:function(result){
                        console.log(result);
                        if(result.code == 100){
                            //   parent.layer.close(index);//关闭当前页
                            layer.msg(result.msg,{
                                icon:1,
                                time:1000
                            });
                            window.location.reload();
                        }
                        else{
                            layer.msg(result.msg,{
                                icon:2,
                                time:1000
                            });
                        }
                    },
                    error:function(err){
                        layer.msg('服务器开小差了！',{
                            time:1500
                        });
                    }
                });
            });
        });
    //二级联动
    $(document).ready(function(e) {
        ChangeType();
        $("#gift-type").change(function(){
            ChangeType();
        });
        $("#typed-id").change(function(){
            ChangeGift();
        })
    });
    function ChangeType(){
        var giftType = $("#gift-type").val();
        console.log(giftType);
        $.ajax({
            async:false,
            url:"/SignRuleGiftName",
            data:{giftType:giftType},
            type:"post",
            dataType:"TEXT",
            success:function(res){
                //  var hang = data.split("|");
                var data = JSON.parse(res);

                console.log(data);
                var str = "<option value='' selected>--请选择--</option>";
                if(data != null){
                    for(var i in data){
                        str+= "<option value="+data[i]['id']+">"+data[i]['gift_name']+"</option>";
                    }
                    // for(var i=0;i<data.length;i++){
                    //     str += "<option value="+data[i].id+">"+data[i].gift_name+"</option>";
                    // }
                }

                $("#typed-id").html(str);
            }
        });
    }
    function ChangeGift(){
        var giftId = $("#typed-id").val();
//        var postData = $('#form').serialize();
//        console.log(giftId);
//        console.log(postData);
    }

//添加自定义内容
    $('#btn-submit').click(function(){
        console.log($('#form').serialize());
        layer.confirm('确定添加自定义数据吗？',{
            title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
            btn: ['确定','取消'] //按钮
        },function(){
            $.ajax({
                type:'post',
                url:'/SignRuleAddDefineContent',
                async:true,
                data:$('#form').serialize(),
                dataType:'json',
                success:function(result){
                    console.log(result.data);
                    console.log(result.res);
                    if(result.code == 100){
                        //   parent.layer.close(index);//关闭当前页
                        layer.msg(result.msg,{
                            icon:1,
                            time:1000
                        });
                        window.location.reload();
                        //$('#tb_departments',parent.window.document).reload();
//                        setTimeout(function () {
//                            parent.layer.close(index);// 关闭layer
//                        },1500);

                        //window.parent.parent.location.replace(location.href);
                        //$('#tb_departments',parent.window.document).bootstrapTable(('refresh'));

                        // window.parent.location.replace(location.href)//刷新父级页面
//                        $('#tb_departments').bootstrapTable(('refresh'));
//                        $('#batch-deal-with').css('display','none');
                    }
                    else{
                        layer.msg(result.msg,{
                            icon:2,
                            time:1000
                        });
                    }
                },
                error:function(err){
                    layer.msg('服务器开小差了！',{
                        time:1500
                    });
                }
            });
        });
    });


</script>
</body>
</html>